package io.kiki.stack.http.feign.SpringMvcContract.annotation;

import io.kiki.stack.http.feign.MethodMetadata;
import io.kiki.stack.http.feign.SpringMvcContract.AnnotatedParameterProcessor;
import io.kiki.stack.http.feign.SpringMvcContract.SpringQueryMap;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * {@link SpringQueryMap} parameter processor.
 */
public class QueryMapParameterProcessor implements AnnotatedParameterProcessor {

    private static final Class<SpringQueryMap> ANNOTATION = SpringQueryMap.class;

    @Override
    public Class<? extends Annotation> getAnnotationType() {
        return ANNOTATION;
    }

    @Override
    public boolean processArgument(AnnotatedParameterContext annotatedParameterContext, Annotation annotation, Method method) {
        int paramIndex = annotatedParameterContext.getParameterIndex();
        MethodMetadata methodMetadata = annotatedParameterContext.getMethodMetadata();
        if (methodMetadata.queryMapIndex() == null) {
            methodMetadata.queryMapIndex(paramIndex);
        }
        return true;
    }

}
